//
//  EQNavigationBarProtocol.swift
//  EQQuestionBank
//
//  Created by Macx on 2018/8/4.
//  Copyright © 2018年 FanWeiJian. All rights reserved.
//

import UIKit
import SnapKit
import RxSwift

protocol EQNavigationBarProtocol {
    
}

extension EQNavigationBarProtocol where Self : UIViewController{
    
    func setNavigationTitle(title : String ,titleColor: UIColor){
        let nav = self.navigationBar()
        let label = UILabel()
        label.text = title
        label.textColor = titleColor
        label.textAlignment = .center
        nav.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.left.equalTo(50)
            make.right.equalTo(-50)
            make.bottom.equalToSuperview()
            make.top.equalTo(20)
        }
    }
    
    
    func setBackButton(){
        let nav = self.navigationBar()
        let button = UIButton()
        button.setImage(#imageLiteral(resourceName: "back"), for: .normal)
        button.contentMode = .scaleAspectFit
        nav.addSubview(button)
        button.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.bottom.equalToSuperview()
            make.width.equalTo(50)
        }
        button.rx.controlEvent(.touchUpInside)
            .subscribe {[weak self] (_) in
                self!.navigationController?.popViewController(animated: true)
            }.disposed(by: rx.disposeBag)
    }
    
    
    func navigationBar() -> UIView {
        var nav : UIView! = nil
        self.view.subviews.forEach { (subView) in
            if subView.tag == EQViewTag.navigationBarView.rawValue {
                nav = subView
                return
            }
        }
        if nav == nil {
            nav = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 64))
            nav.backgroundColor = .white
            nav.tag = EQViewTag.navigationBarView.rawValue
            self.view.addSubview(nav)
        }

        return nav
    }
    
}

